#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int n, A[200005], d;
ll res=0;
main(){
    ios::sync_with_stdio(0);
    cin.tie(nullptr);
    if (fopen("hoixuan.inp","r")){
        freopen("hoixuan.inp","r",stdin);
        freopen("hoixuan.out","w",stdout);
    }
    cin >> n >> d;
    for (int i=1; i<=n; i++){
        cin >> A[i];
    }
    sort(A+1,A+n+1);
    ll j=1;
    for (ll i=3; i<=n; i++){
        while (A[i]-A[j]>d){
            j++;
        }
        res+=(i-j)*(i-j-1ll)/2;
    }
    cout << res;
}
